Django Channels 实现点对点实时聊天和消息推送功能

您所在的位置:网站首页 django 聊天 Django Channels 实现点对点实时聊天和消息推送功能

Django Channels 实现点对点实时聊天和消息推送功能

2024-07-11 22:14| 来源: 网络整理| 查看: 265

下面我会详细讲解如何使用 Django Channels 实现点对点实时聊天和消息推送功能。这里的示例要求你已经安装了 Django 3.x 和 Django Channels 3.x。

添加依赖

在使用 Django Channels 之前,需要安装一些依赖:

pip install channels channels_redis gunicorn

其中,channels 是 Django Channels 的核心依赖,channels_redis 是使用 Redis 作为 Channels 的层级系统所需的依赖。

修改配置

接下来在 Django 项目的配置文件中添加 Channels 的配置:

# settings.py INSTALLED_APPS = [ # ... 'channels', ] ASGI_APPLICATION = 'myproject.asgi.application'

ASGI_APPLICATION 是 Django Channels 设置的入口(实例)。

创建 myproject/asgi.py 文件,该文件的作用是创建并返回 WebSocket 类的实例,以支持 Channels 的异步协议:

# myproject/asgi.py import os from django.core.asgi import get_asgi_application from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from myapp import routing os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') application = ProtocolTypeRouter({ "http": get_asgi_application(), "websocket": AuthMiddlewareStack( URLRouter( routing.websocket_urlpatterns ) ), })

需要注意的是,这里的 routing 对象是自定义的路由规则,需要手动创建。我们可以在 myapp/routing.py 中定义路由规则:

# myapp/routing.py from django.urls import re_path from . import consumers websocket_urlpatterns = [ re_path(r'chat/(?P\w+)/$', consumers.ChatConsumer.as_asgi()), ]

以上是一个简单的路由规则,定义了连接 WebSocket 服务的 url 格式。

编写 Consumer

Consumer 是聊天室的业务实现类,我们可以在 myapp/consumers.py 中编写它:

import asyncio from channels.generic.websocket import AsyncWebsocketConsumer class ChatConsumer(AsyncWebsocketConsumer): async def connect(self): self.room_name = self.scope['url_route']['kwargs']['room_name'] self.room_group_name = 'chat_%s' % self.room_name # 加入聊天室 await self.channel_layer.group_add(self.room_group_name, self.channel_name) await self.accept() async def disconnect(self, close_code): # 离开聊天室 await self.channel_layer.group_discard(self.room_group_name, self.channel_name) async def receive(self, text_data): # 接收消息并发送到聊天室的所有人 await self.channel_layer.group_send( self.room_group_name, { 'type': 'chat_message', 'message': text_data } ) async def chat_message(self, event): # 从聊天室中收到消息后转发给当前客户端 message = event['message'] await self.send(text_data=message)

这个 Consumer 能实现的功能是:

连接到聊天室 离开聊天室 接收消息并发送到聊天室 接收到来自频道的消息并转发到客户端 发送消息

为了发送挂起的消息,需要在 Django 的 settings.py 文件中添加一个配置:

# settings.py CHANNEL_LAYERS = { "default": { "BACKEND": "channels_redis.core.RedisChannelLayer", "CONFIG": { "hosts": [("127.0.0.1", 6379)], }, "ROUTING": "myproject.routing.application", }, }

上面这段配置告诉 Channels 用 Redis 做层级系统,并且包含一个名为“default”的虚拟频道。

最重要的是,这里配置了 Channels 在接下来的消息传递中使用的协议,定义为 Redis。我们在后台启动 Redis 服务器,并将配置更新到 myapp/views.py 文件的视图函数:

# myapp/views.py from django.shortcuts import render from django.http import JsonResponse from myapp.consumers import ChatConsumer def greet(request): # 发送消息给用户 async_to_sync(ChatConsumer.send)( {'type': 'chat_message', 'message': 'Hello World!'} ) return JsonResponse({'status': 'success'})

这里我们在视图函数中,简单地发送了一条消息,发送消息的 type 字段应该是上面定义的回调函数名,这里是 chat_message()。

以上是使用 Django Channels 实现点对点实时聊天和消息推送功能的完整攻略。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Django Channels 实现点对点实时聊天和消息推送功能 - Python技术站



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3